import {debounce} from "../common/auth";
import pub from "@/common/pub"

/**
 * 使用proxyGoods类后无需关注修改数量价格后数量计算问题 减少代码量
 * @author jjw
 */
class ProxyGoods {}
ProxyGoods.prototype.changeCount = function (pop) {
    const _boxUnitCount = Number(this.boxUnitCount)  >> 0
    if(!_boxUnitCount)throw Error('未配置换算率');
    if(pop === 'goodsCount'){
        this.boxNum = (this.goodsCount / _boxUnitCount) >> 0
        this.mainNum = this.goodsCount % _boxUnitCount
    }else {
        this[pop] = Number(this[pop])
        //改变零数计算数量
        let mainNum = 0;
        let boxNum = 0;
        //判断当前零数是否大于箱单位计量数
        if(this.mainNum >= this.boxUnitCount){
            mainNum = this.mainNum % _boxUnitCount
            boxNum = (this.mainNum / _boxUnitCount) >> 0
            this.boxNum = boxNum + this.boxNum;
            this.mainNum = mainNum;
        }
        this.taxBoxAmount = this.boxNum * this.taxBoxPrice
        this.taxMainAmount = this.mainNum * this.taxPrice
        this.goodsCount = this.boxNum * this.boxUnitCount + this.mainNum
        this.noTaxAmount = Number(this.noTaxPrice ) * Number(this.goodsCount || 0).toFixed(6)
    }
    this.taxAmount = this.goodsCount * this.taxPrice;

     this.strategy && this.strategy()
    //数据更新
    pub.$pub('updateGoodsData',this)
    //如果该商品是满赠则发布订阅 通知ShoppingCardList进行更新
    if(this.promotionType === '3'){
        pub.$pub('changePromotion',this)
    }
}
//计算销售金额
ProxyGoods.prototype.changeAmount = function (pop) {
        //暂时不做促销
    //没有箱单位计量数不做计算
    if(!this.boxUnitCount)return;
        //金额算单价
    if(pop === 'taxAmount'){
        this.taxPrice = (Number(this.taxAmount || 0) / Number(this.goodsCount || 0)).toFixed(6)
        this.taxBoxPrice = Number(this.taxPrice || 0) * Number(this.boxUnitCount || 0).toFixed(6)
        this.noTaxPrice = Number(this.taxPrice || 0) / (1 + Number(this.taxRate || 0) / 100).toFixed(6)
        this.noTaxBoxPrice = Number(this.taxBoxPrice || 0) / (1 + Number(this.taxRate || 0) / 100).toFixed(6)
        this.noTaxAmount = Number(this.noTaxPrice || 0) * Number(this.goodsCount || 0).toFixed(6)
        this.tax = Number(this.taxAmount || 0) - Number(this.noTaxAmount || 0).toFixed(6)
        this.billTaxPrice = this.taxPrice
        this.billTaxBoxPrice = this.taxBoxPrice
    }else if(pop === 'taxBoxPrice'){
        this.taxPrice = (Number(this.taxBoxPrice) / (Number(this.boxUnitCount))).toFixed(6)
        this.noTaxPrice = (Number(this.taxPrice ) / (1 + Number(this.taxRate) / 100)).toFixed(6)
        this.noTaxBoxPrice = (Number(this.taxBoxPrice) / (1 + Number(this.taxRate) / 100)).toFixed(6)
        this.noTaxAmount = Number(this.noTaxPrice * this.goodsCount).toFixed(6)
        this.taxAmount = (Number(this.taxPrice || 0) * Number(this.goodsCount || 0)).toFixed(6)
        this.tax = (Number(this.taxAmount || 0) - Number(this.noTaxAmount || 0)).toFixed(6)
    }else{
        this.taxBoxPrice = Number(this.taxPrice || 0) * Number(this.boxUnitCount || 0).toFixed(6)
        this.noTaxPrice = Number(this.taxPrice) / (1 + Number(this.taxRate) / 100).toFixed(6)
        this.noTaxBoxPrice = Number(this.taxBoxPrice ) / (1 + Number(this.taxRate) / 100).toFixed(6)
        this.taxAmount = Number(this.taxPrice || 0) * Number(this.goodsCount || 0).toFixed(6)
        this.noTaxAmount = Number(this.noTaxPrice || 0) * Number(this.goodsCount || 0).toFixed(6)
        this.tax = Number(this.taxAmount || 0) - Number(this.noTaxAmount || 0).toFixed(6)
    }
    this.taxBoxAmount = this.boxNum * this.taxBoxPrice
    this.taxMainAmount = this.mainNum * this.taxPrice
    this.billTaxPrice = this.taxPrice
    this.billTaxBoxPrice = this.taxBoxPrice
    this.billTaxAmount = Number(this.billTaxPrice) * Number(this.goodsCount)
    this.strategy && this.strategy()
    //价格变动不触发订阅
    //数据更新
    pub.$pub('updateGoodsData',this)
}

// //计算采购金额
// ProxyGoods.prototype.changeAmount = function (pop) {
//     //暂时不做促销
//     //没有箱单位计量数不做计算
//     if(!this.boxUnitCount)return;
//     //金额算单价
//     if(pop === 'buyTaxAmount'){
//         this.buyTaxPrice = (Number(this.buyTaxAmount || 0) / Number(this.goodsCount || 0)).toFixed(6)
//         this.taxBoxPrice = Number(this.buyTaxPrice || 0) * Number(this.boxUnitCount || 0).toFixed(6)
//         this.buyNoTaxPrice = Number(this.buyTaxPrice || 0) / (1 + Number(this.taxRate || 0) / 100).toFixed(6)
//         this.buyNoTaxBoxPrice = Number(this.buyTaxBoxPrice || 0) / (1 + Number(this.taxRate || 0) / 100).toFixed(6)
//         this.buyNoTaxAmount = Number(this.buyNoTaxPrice || 0) * Number(this.goodsCount || 0).toFixed(6)
//         this.tax = Number(this.buyTaxAmount || 0) - Number(this.buyNoTaxAmount || 0).toFixed(6)
//     }else if(pop === 'buyTaxBoxPrice'){
//         this.buyTaxPrice = (Number(this.buyTaxBoxPrice) / (Number(this.boxUnitCount))).toFixed(6)
//         this.buyNoTaxPrice = (Number(this.buyTaxPrice ) / (1 + Number(this.taxRate) / 100)).toFixed(6)
//         this.buyNoTaxBoxPrice = (Number(this.buyTaxBoxPrice) / (1 + Number(this.taxRate) / 100)).toFixed(6)
//         this.buyNoTaxAmount = Number(this.buyNoTaxPrice * this.goodsCount).toFixed(6)
//         this.buyTaxAmount = (Number(this.buyTaxPrice || 0) * Number(this.goodsCount || 0)).toFixed(6)
//         this.tax = (Number(this.buyTaxAmount || 0) - Number(this.buyNoTaxAmount || 0)).toFixed(6)
//     }else{
//         this.buyTaxBoxPrice = Number(this.buyTaxPrice || 0) * Number(this.boxUnitCount || 0).toFixed(6)
//         this.buyNoTaxPrice = Number(this.buyTaxPrice) / (1 + Number(this.taxRate) / 100).toFixed(6)
//         this.buyNoTaxBoxPrice = Number(this.buyTaxBoxPrice ) / (1 + Number(this.taxRate) / 100).toFixed(6)
//         this.buyTaxAmount = Number(this.buyTaxPrice || 0) * Number(this.goodsCount || 0).toFixed(6)
//         this.buyNoTaxAmount = Number(this.buyNoTaxPrice || 0) * Number(this.goodsCount || 0).toFixed(6)
//         this.tax = Number(this.buyTaxAmount || 0) - Number(this.buyNoTaxAmount || 0).toFixed(6)
//     }
//     this.strategy && this.strategy()
//     //价格变动不触发订阅
//     //数据更新
//     pub.$pub('updateGoodsData',this)
// }

//计算体积
ProxyGoods.prototype.changeVolume = function (pop) {
    this.volume = (Number(this.length) * Number(this.width) * Number(this.height)).toFixed(6)
    pub.$pub('updateGoodsData',this)
}
//计算箱单价



// //变更需要计算数量的属性
// const countType = ['mainNum','boxNum','goodsCount']
// //变更需要计算金额的属性
// const amountType = ['taxPrice','taxBoxPrice','taxAmount']
//
// //计算体积的属性
// const volumeType = ['length','width','height']


const computeAttribute = {
    mainNum:"changeCount",
    boxNum:"changeCount",
    goodsCount:"changeCount",
    taxPrice:"changeAmount",
    taxBoxPrice:"changeAmount",
    taxAmount:"changeAmount",
    length:"changeVolume",
    width:"changeVolume",
    height:"changeVolume"
}

const proxyGoods = function (goods,strategy) {
    const curGoods = Object.assign(new ProxyGoods(),goods)
    const proxyGoods = new Proxy(curGoods,{

        set(target, p, newValue, receiver) {
            const isUp = Reflect.set(target, p, newValue, receiver)
            if(isUp && computeAttribute[p]){
                target[computeAttribute[p]](p)
            }
            return isUp
        }
    })
    if(strategy){
        proxyGoods.strategy = strategy
    }
    return proxyGoods
}
export default proxyGoods
